// $Id$

using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithms.Numerical
{
    public class IntMatrix
    {
        protected readonly int height;
        protected readonly int width;
        protected int[ , ] matrix;

        public IntMatrix( int height, int width )
        {
            this.height = height;
            this.width = width;
            matrix = new int[ height, width ];
        }

        public IntMatrix( int[,] m )
        {
            matrix = m;
            height = matrix.GetLength( 0 );
            width = matrix.GetLength( 1 );
        }

        public int this[ int y, int x ]
        {
            get { return matrix[ y, x ]; }
            set { matrix[ y, x ] = value; }
        }

        public IntMatrix Transponed
        {
            get
            {
                int[,] m = new int[ width, height ];

                for( int x = 0; x < width; x++ )
                    for( int y = 0; y < height; y++ )
                        m[ x, y ] = matrix[ y, x ];

                return new IntMatrix( m );
            }
        }

        public Matrix Matrix
        {
            get
            {
                Matrix r = MatrixFactory.Instance.MakeZero(this.Height, this.Width);
                for (int y = 0; y < height; y++)
                    for (int x = 0; x < width; x++)
                        r[y, x] = this[y, x];
                return r;
            }
        }

        public int[,] Array
        {
            get { return matrix; }
        }

        public int Width
        {
            get { return width; }
        }

        public int Height
        {
            get { return height; }
        }

        public int[] getRow(int r)
        {
            int[] row = new int[width];

            for (int x = 0; x < width; x++)
                row[x] = matrix[r, x];

            return row;
        }

        public int[] getColumn(int c)
        {
            int[] column = new int[height];

            for (int x = 0; x < height; x++)
                column[x] = matrix[x, c];

            return column;
        }

        public override string ToString()
        {
            string s = "[ \n";
            for (int y = 0; y < Height; y++)
            {
                s += "  [";
                for (int x = 0; x < Width; x++)
                {
                    s += this[y, x].ToString("N");
                    if (x < (Width - 1))
                        s += ", ";
                }
                s += " ]\n";
            }

            s += "]\n";

            return s;
        }

    
    }
}
